/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package alpes.aerolinea;

import alpes.aerolinea.entities.ServicioVideoSeries;
import alpes.aerolinea.services.ISeriesDeTvService;
import alpes.aerolinea.services.SeriesDeTvService;
import alpes.aerolineas.exceptions.ExisteException;
import alpes.aerolineas.exceptions.NoExisteException;
import java.util.Hashtable;
import javax.faces.event.ActionEvent;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Daniel
 */
public class SeriesDeTvMenuBeanAdministrador {

    private final static String ERROR = "ERROR";

    private List<ServicioVideoSeries> series;
    @EJB
    private ISeriesDeTvService seriesDeTvService;
    private ServicioVideoSeries serie;
    private String nombreSerie;
    private String id;

    /** Creates a new instance of SeriesDeTvMenuBeanAdministrador */
    public SeriesDeTvMenuBeanAdministrador() {
        //seriesDeTvService = lookupService();
    }

    @PostConstruct
    public void init() {
        series = seriesDeTvService.findAll();
        serie = seriesDeTvService.getSerie();
    }

    public List<ServicioVideoSeries> getAllSeriesTv() {
        return series;
    }

    public ServicioVideoSeries getSerie() {
        return serie;
    }

    public void setSerie(ServicioVideoSeries serie) {
        this.serie = serie;
    }

    public String createOrUpdate() {
        try {
            System.out.println("1");
            serie = seriesDeTvService.createOrUpdate();
            destroyBean();
            return null;
        } catch (Exception ex) {
            FacesContext context = FacesContext.getCurrentInstance();
            String msg = ex.getMessage();
            FacesMessage fm = new FacesMessage(msg);
            fm.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage(null, fm);
            Logger.getLogger(logInBean.class.getName()).log(Level.SEVERE, null, ex);
            return ERROR;
        }
    }

    public String newSerie() {
        serie = seriesDeTvService.newSerie();
        System.out.println("Nueva serie!!");
        return null;
    }

    public void delete(ActionEvent event) {
        UIParameter component = (UIParameter)event.getComponent().findComponent("nombreSerie");
        nombreSerie = component.getValue().toString();
        System.out.println("delete series " + nombreSerie);
        seriesDeTvService.setNombre(nombreSerie);
        seriesDeTvService.delete();
        destroyBean();
    }

    /**
     * Elimina la instancia actual del bean.
     */
    public void destroyBean() {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("SeriesDeTvMenuBeanAdministrador");
    }

    /**
     * asigna la serie que actualmente este seleccionada.
     * @param event
     * @return resultado de la operacion 
     */
    public String findByNombre(ActionEvent event) {
        UIParameter component = (UIParameter)event.getComponent().findComponent("nombreSerie0");
        nombreSerie = component.getValue().toString();
        serie = seriesDeTvService.setNombre(nombreSerie);
        System.out.println("SERIE:" + serie);
        return null;
    }
        /**
     * asigna la serie que actualmente este seleccionada.
     * @param event
     * @return resultado de la operacion
     */
    public String findByID(ActionEvent event) {
        try {
            UIParameter component = (UIParameter) event.getComponent().findComponent("nombreSerie0");
            id = component.getValue().toString();
            System.out.println("KK: " + id);
            serie = seriesDeTvService.setIdentification(id);
            System.out.println("SERIE:" + serie);
            return null;
        } catch (Exception ex) {
            FacesContext context = FacesContext.getCurrentInstance();
            String msg = ex.getMessage();
            FacesMessage fm = new FacesMessage(msg);
            fm.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage(null, fm);
            Logger.getLogger(logInBean.class.getName()).log(Level.SEVERE, null, ex);
            return ERROR;
        }
    }

    public String getNombreSerie() {
        return nombreSerie;
    }

    public void setNombreSerie(String nombreSerie) {
        this.nombreSerie = nombreSerie;
    }

    private ISeriesDeTvService lookupService() {
        try{
        Hashtable<String, String> environment = new Hashtable<String, String>();
            environment.put("java.naming.factory.initial","com.sun.enterprise.naming.SerialInitContextFactory");
            environment.put("java.naming.factory.url.pkgs","com.sun.enterprise.naming");
            environment.put("java.naming.factory.state"," com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
            environment.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
            environment.put("org.omg.CORBA.ORBInitialHost","localhost");
            environment.put("org.omg.CORBA.ORBInitialPorts","3700");
            Context c = new InitialContext(environment);
            return (ISeriesDeTvService)c.lookup("alpes.aerolinea.services.ISeriesDeTvService");
        }
        catch(NamingException ne)
        {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught",ne);
            throw new RuntimeException(ne);
        }
    }
}
